package kr.co.happenstance;

import java.util.Locale;

import kr.co.happenstance.util.AndroidUtil;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

import android.content.Intent;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;

public class HomeActivity extends BaseActivity implements View.OnClickListener{
	protected final String TAG = "<<<HomeActivity>>>";
	
	/**
	 * 최초 back 버튼 클릭시 2초후 메세지를 받는 Handler 객체<br><br>
	 * 
	 * 메세지를 받을 경우 finishFlag를 false로 셋팅
	 * 
	 */
	private Handler finishHandler;
	
	/**
	 * back 버튼 클릭시 이 플래그값에 따라 어플 종료 또는 Toast 메세지 출력후 finishHandler에게 메세지 전송<br><br>
	 * 
	 * true : 어플종료<br>
	 * false : Toast 메세지 출력후 finishHandler에게 메세지 전송<br>
	 */
	private boolean finishFlag;		
	
	
	/**
	 * GoogleMap 객체
	 */
	private GoogleMap googleMap;
	
	/**
	 * 주소 TextView 객체
	 */
	private TextView tvAddress;
	
	/**
	 * 프로필전송 CheckBox 객체
	 */
	private CheckBox chkProfile;	
	

	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_home);
		
		tvAddress = (TextView) this.findViewById(R.id.tv_address);
		chkProfile = (CheckBox) this.findViewById(R.id.chk_profile);
		
		init(this, C.MENU_ID_HOME);
		settingMapAndAddress();
		
		finishHandler = new Handler(new FinishHandlerCallback());
		
	}
	
	@Override
	public void onBackPressed(){
		if(!finishFlag){
			Toast.makeText(this, R.string.application_finish_msg, Toast.LENGTH_SHORT).show();
			finishFlag = true;
			
			//2초후 핸들러에게 메세지 전달
			finishHandler.sendEmptyMessageDelayed(0, 1000*2);
		}else{
			super.onBackPressed();
			finish();
			android.os.Process.killProcess(android.os.Process.myPid());
			
		}
	}	
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	@Override
	public void onClick(View v) {
		switch(v.getId()){
			case R.id.btn_meet:
				Log.d(TAG, "btn_meet onClick...............");
				
				Intent intent = new Intent(this, MeetActivity.class);
				intent.putExtra("profileYN", chkProfile.isChecked()?"Y":"N");
				startActivity(intent);
				break;
		}
	}	
	
	/**
	 * Google Map 셋팅 메소드
	 * 
	 * <li>Map 좌표 현재 나의 위치로 이동</li>
	 * <li>Map 축척 변경</li>
	 * <li>Marker 표시</li>
	 * <li>주소 표시</li>
	 * 
	 */
	private void settingMapAndAddress(){
		if(GooglePlayServicesUtil.isGooglePlayServicesAvailable(this) == ConnectionResult.SUCCESS && myLocation != null){
			
			LatLng position = new LatLng(myLocation.getLatitude(), myLocation.getLongitude());
			googleMap = ((SupportMapFragment) getSupportFragmentManager()	.findFragmentById(R.id.map)).getMap();
			
			//맵 위치이동.
			googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(position, 15));
			
			MarkerOptions markerOptions = new MarkerOptions();
			markerOptions.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker));
			markerOptions.position(position);
			
			//마커 설정.
			googleMap.addMarker(markerOptions).showInfoWindow();
			
			tvAddress.setText(AndroidUtil.getAddress(this, Locale.getDefault(), myLocation.getLatitude(), myLocation.getLongitude()));
			tvAddress.setSelected(true);
		}
	}
	


	/**
	 * 메모리 릭 방지을 위해서 구현한 Handler.Callback 클래스
	 * 
	 * @author Administrator
	 *
	 */
	private class FinishHandlerCallback implements Handler.Callback{
		@Override
		public boolean handleMessage(Message msg) {
			if(msg.what == 0){
				finishFlag = false;
			}			
			return true;
		}
		
	}	
 	
}
